package com.truongnha.clienttn.data.dao;

import java.util.ArrayList;
import java.util.List;

import com.truongnha.clienttn.data.entity.AttendEntity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AttendDAO {
	private final SQLiteDatabase db;

	public AttendDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}

	public long insert(String type, String sent, String time) {
		ContentValues inititalValue = new ContentValues();
		inititalValue.put(AttendScheme.TYPE, type);
		inititalValue.put(AttendScheme.SENT, sent);
		inititalValue.put(AttendScheme.TIME, time);
		return db.insert(AttendScheme.TABLE_NAME, null, inititalValue);
	}

	public void delete() {
		db.delete(AttendScheme.TABLE_NAME, null, null);
	}

	public List<AttendEntity> getAll() {
		String sql = "SELECT * from " + AttendScheme.TABLE_NAME;
		Cursor mcursor = db.rawQuery(sql, null);
		List<AttendEntity> list = new ArrayList<AttendEntity>();

		if (mcursor.getCount() != 0) {
			mcursor.moveToFirst();
			while (!mcursor.isAfterLast()) {
				AttendEntity attendEntity = new AttendEntity(mcursor.getInt(0),
						mcursor.getString(1), mcursor.getString(2),
						mcursor.getString(3));
				list.add(attendEntity);
				mcursor.moveToNext();
			}
		}
		mcursor.close();
		return list;
	}
}
